package utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import uk.ac.nott.cs.g53dia.bee.BeeSimulator;
import valueobj.SimulationSettings;

public class PropertiesHandler {

  public PropertiesHandler() {

  }

  private Map<String, String> loadAll(String path) {
    boolean local = Constants.PROPERTIES_PATH.equals(path);
    Map<String, String> result = load(path, local);
    if (result.get(Constants.SIMULATION_CFG_KEY) != null) {
      result.putAll(load(Constants.PROPERTIES_PATH_PREFIX + result.get(Constants.SIMULATION_CFG_KEY) + ".properties",
          local));
    }
    return result;
  }

  private Map<String, String> load(String path, boolean local) {
    InputStream is = null;
    if (local) {
      is = BeeSimulator.class.getClass().getResourceAsStream(path);
    } else {
      try {
        is = new FileInputStream(path);
      } catch (FileNotFoundException e2) {
        System.err.println("File with configuration has not been found.");
        System.exit(1);
        e2.printStackTrace();
      }
    }

    Properties pr = new Properties();
    try {
      pr.load(is);
    } catch (IOException e1) {
      System.err.println("Unable to open " + path);
      e1.printStackTrace();
    } finally {
      try {
        if (is != null) {
          is.close();
        }
      } catch (IOException e) {
        System.err.println("Unable to close " + Constants.PROPERTIES_PATH);
        e.printStackTrace();
      } finally {
        is = null;
      }
    }
    Map<String, String> resultMap = new HashMap<String, String>();
    for (Object o : pr.keySet()) {
      String key = (String) o;
      resultMap.put(key, pr.getProperty(key));
    }

    return resultMap;
  }

  public SimulationSettings getSettings() {
    return getSettings(Constants.PROPERTIES_PATH);
  }

  public SimulationSettings getSettings(String path) {
    Map<String, String> prop = loadAll(path);
    int tick = Constants.TICK_DELAY_DEFAULT;
    int beesNum = Constants.BEES_NUMBER_DEFAULT;
    boolean gui = Constants.GUI_DEFAULT;
    boolean record = Constants.RECORD_DEFAULT;
    long randomSeed = Constants.RANDOM_SEED_DEFAULT;
    int duration = Constants.DURATION_DEFAULT;
    try {
      tick = Integer.parseInt(prop.get(Constants.TICK_DELAY_KEY));
      beesNum = Integer.parseInt(prop.get(Constants.BEES_NUMBER_KEY));
      gui = "true".equals(prop.get(Constants.GUI_KEY));
      record = "true".equals(prop.get(Constants.RECORD_KEY));
      randomSeed = Long.parseLong(prop.get(Constants.RANDOM_SEED_KEY));
      duration = Integer.parseInt(prop.get(Constants.DURATION_KEY));
    } catch (NumberFormatException nfe) {
      System.err.println("Error when reading configuration files.");
    }
    SimulationSettings s = new SimulationSettings();
    s.setBeesNum(beesNum);
    s.setGui(gui);
    s.setRandomSeed(randomSeed);
    s.setTick(tick);
    s.setDuration(duration);
    s.setRecord(record);
    return s;
  }
}
